﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient; 
namespace hissql
{
    public partial class doc_huanzhechaxun : Form
    {
       
        public doc_huanzhechaxun()
        {
            InitializeComponent();
        }
        private DataTable SelectedYaopingTable;
        private DataTable YaopingTable;
        SqlDataAdapter sqlda1 = new SqlDataAdapter("Select * from tb_Medicine ", sqlHelper.sqlConnection);
             DataSet myds1 = new DataSet();
         
  

        private void doc_huanzhechaxun_Load(object sender, EventArgs e)
        {
            SqlDataAdapter sqlda = new SqlDataAdapter("Select No,DateTime,Time,PatientNo from tb_Invoice where DoctorName='" + UserHelper.name+"'", sqlHelper.sqlConnection);
             DataSet myds = new DataSet();
             sqlda.Fill(myds, "tabName");
            dgv_huanzhe.DataSource = myds.Tables["tabName"];

             //sqlda1.Fill(myds1, "tabName");
            // dgv_yaoping.DataSource = myds1.Tables["tabName"];
             
           

             SqlCommand sqlCommand = new SqlCommand();                                                       //声明并实例化SQL命令；
             sqlCommand.Connection = sqlHelper.sqlConnection;                                                          //将SQL命令的连接属性指向SQL连接；
             sqlHelper.sqlConnection.Open();
             sqlCommand.CommandText =
                 "Select * from tb_Medicine ";                  //指定SQL命令的命令文本；该命令查询学生尚未选修的课程，以用作数据网格视图数据源；
             sqlCommand.Parameters.AddWithValue("@PatientNo", tb_no.Text.Trim());                                 //向SQL命令的参数集合添加参数的名称、SQL Server数据类型、长度（仅用于定长类型）、所绑定的数据表中的列名；
             SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();                                           //声明并实例化SQL数据适配器；
             sqlDataAdapter.SelectCommand = sqlCommand;                                                      //将SQL数据适配器的查询命令属性指向SQL命令；
             this.YaopingTable = new DataTable();                                                             //实例化本窗体的课程数据表，用于保存所有课程，以用作数据网格视图数据源；
             this.SelectedYaopingTable = new DataTable();                                                     //实例化本窗体的已选课程数据表，用于保存学生已选修课程，以用作数据网格视图数据源；
             sqlHelper.sqlConnection.Close();
             sqlDataAdapter.Fill(this.YaopingTable);                                                          //SQL数据适配器读取数据，并填充课程数据表；
         
            sqlCommand.CommandText =
                 "Select * from tb_PM"
                 + " WHERE PatientNo=@PatientNo";
             //指定SQL命令的命令文本；该命令查询学生已选修的课程，以用作数据网格视图数据源；
             this.dgv_xuanzhongyaoping.Columns.Clear(); 
             sqlDataAdapter.Fill(this.SelectedYaopingTable);                                                  //SQL数据适配器读取数据，并填充已选课程数据表； 
             this.dgv_xuanzhongyaoping.DataSource = this.SelectedYaopingTable;                                  //将数据网格视图的数据源设为学生数
             this.dgv_yaoping.DataSource = this.YaopingTable;
        }

        private void button1_Click(object sender, EventArgs e)
        {
           
           
            if (this.dgv_yaoping.RowCount > 0)                                                               //若课程数据网格视图内的行计数大于0；
            {
                DataRow                                                                                     //声明数据行；
                    currentYaopingRow = ((DataRowView)this.dgv_yaoping.CurrentRow.DataBoundItem).Row          //当前课程数据行可通过课程数据网格视图的当前行的数据绑定项获得，后者可直接转换为数据行视图，从而获取其相应的数据行；
                    , selectedYaopingRow = this.SelectedYaopingTable.NewRow();                                //已选课程数据行则通过已选课程数据表的方法NewRow来新建；随后该行的状态为附加；
                selectedYaopingRow["MedicineNo"] = currentYaopingRow["No"];                                           //逐一将当前课程数据行的各列值，赋予已选课程数据行的相应列；
                selectedYaopingRow["Name"] = currentYaopingRow["Name"];
                selectedYaopingRow["Price"] = currentYaopingRow["Price"];
                //selectedYaopingRow["Standard"] = currentYaopingRow["Standard"];
                selectedYaopingRow["Unit"] = currentYaopingRow["Unit"];
                selectedYaopingRow["PatientNo"] = tb_no.Text .Trim ();
                this.SelectedYaopingTable.Rows.Add(selectedYaopingRow);                                       //已选课程数据行加入已选课程数据表；随后该行的状态为添加；
                currentYaopingRow.Delete();                                                                  //当前课程数据行删除；随后该行的状态为删除；
               // this.lb_zongjia .Text =                                                                   //在标签中显示已选课程的学分总和；
                   // "共" + this.SelectedYaopingTable.Compute("SUM((float)Price)", "").ToString() + "元";         //借助已选课程数据表的方法Compute，实现简单计算，例如聚合；
                
            }

        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            DocMain f = new DocMain();
            f.Show();
            this.Close();
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
           SqlDataAdapter sqlda1 = new SqlDataAdapter("Select No,Name,Price from tb_Medicine where Name  LIKE '%" + textBox2.Text.Trim() + "%'", sqlHelper.sqlConnection);
            DataSet myds1 = new DataSet();
            sqlda1.Fill(myds1, "tabName1");
            dgv_yaoping.DataSource = myds1.Tables["tabName1"];
        }

        private void dgv_yaoping_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void dgv_xuanzhongyaoping_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void bt_shanchu_Click(object sender, EventArgs e)
        {
           
                    if (this.dgv_xuanzhongyaoping.RowCount > 0)                                                               //若课程数据网格视图内的行计数大于0；
                    {
                        DataRow                                                                                     //声明数据行；
                            selectedYaopingRow = ((DataRowView)this.dgv_xuanzhongyaoping.CurrentRow.DataBoundItem).Row          //当前课程数据行可通过课程数据网格视图的当前行的数据绑定项获得，后者可直接转换为数据行视图，从而获取其相应的数据行；
                            ,currentYaopingRow= this.YaopingTable.NewRow();                                //已选课程数据行则通过已选课程数据表的方法NewRow来新建；随后该行的状态为附加；
                         currentYaopingRow["No"]=selectedYaopingRow["MedicineNo"];                                           //逐一将当前课程数据行的各列值，赋予已选课程数据行的相应列；
                         currentYaopingRow["Name"]=selectedYaopingRow["Name"] ;
                        currentYaopingRow["Price"]=selectedYaopingRow["Price"] ;
                        //currentYaopingRow["Standard"]=selectedYaopingRow["Standard"];
                        currentYaopingRow["Unit"] = selectedYaopingRow["Unit"];


                        this.YaopingTable.Rows.Add(currentYaopingRow);                                       //已选课程数据行加入已选课程数据表；随后该行的状态为添加；
                        selectedYaopingRow.Delete();                                                                  //当前课程数据行删除；随后该行的状态为删除；
                        // this.lb_zongjia .Text =                                                                   //在标签中显示已选课程的学分总和；
                        // "共" + this.SelectedYaopingTable.Compute("SUM((float)Price)", "").ToString() + "元";         //借助已选课程数据表的方法Compute，实现简单计算，例如聚合；

                    }
             
        }

        private void bt_tijiao_Click(object sender, EventArgs e)
        {
           
            SqlCommand sqlCommand = new SqlCommand();                                                       //声明并实例化SQL命令；
            sqlCommand.Connection = sqlHelper .sqlConnection;                                                          //将SQL命令的连接属性指向SQL连接；
            sqlCommand.CommandText =
                "INSERT tb_PM(PatientNo,MedicineNo,Name,Standard,Unit,Price)"
                + "VALUES(@PatientNo,@MedicineNo,@Name,@Standard,@Unit,@Price);";                                               //指定SQL命令的命令文本；该命令插入选课记录；
            sqlCommand.Parameters.AddWithValue("@PatientNo", tb_no .Text .Trim ());                                 //向SQL命令的参数集合添加参数的名称、SQL Server数据类型、长度（仅用于定长类型）、所绑定的数据表中的列名；
            sqlCommand.Parameters.Add("@MedicineNo", SqlDbType.Char, 4, "MedicineNo");
            sqlCommand.Parameters.Add("@Name", SqlDbType.Char, 4,  "Name");
            sqlCommand.Parameters.Add("@Standard", SqlDbType.Char, 4, "Standard");
            sqlCommand.Parameters.Add("@Unit", SqlDbType.Char, 4, "Unit");
            sqlCommand.Parameters.Add("@Price", SqlDbType.Char, 4, "Price");
            SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();                                           //声明并实例化SQL数据适配器；
            sqlDataAdapter.InsertCommand = sqlCommand;                                                      //将SQL数据适配器的查询命令属性指向SQL命令；
            sqlHelper.sqlConnection.Open();                                                                           //打开SQL连接；
            int rowAffected = sqlDataAdapter.Update(this.SelectedYaopingTable);                              //SQL数据适配器根据学生数据表提交更新，并返回受影响行数；
            sqlHelper.sqlConnection.Close();                                                                          //关闭SQL连接；
            MessageBox.Show("插入" + rowAffected.ToString() + "行。");                                      //在消息框显示受影响
        }

        private void tb_no_TextChanged(object sender, EventArgs e)
        {
            /*SqlDataAdapter sqlda2 = new SqlDataAdapter("Select * from tb_PM where PatientNo='" + tb_no.Text.Trim() + "'", sqlHelper.sqlConnection);
            DataSet myds2 = new DataSet();
            sqlda2.Fill(myds2, "tabName");
            dgv_xuanzhongyaoping.DataSource = myds2.Tables["tabName"];*/
        }

        private void button1_Click_1(object sender, EventArgs e)
        {

            SqlCommand sqlCommand = sqlHelper.sqlConnection.CreateCommand();
            sqlCommand.CommandText =
                            "update tb_Patient set Weiyue=1+Weiyue where No='" + tb_no.Text + "'";

            sqlHelper.sqlConnection.Open();
            int rowAffected = sqlCommand.ExecuteNonQuery();
            if (rowAffected == 1)                                                                    //若成功写入1行记录；
            {
                MessageBox.Show("添加成功！");                                                    //给出正确提示；
            }
            sqlHelper.sqlConnection.Close();
        }

        private void 个人信息ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DocPI f = new DocPI();
            f.Show();
            this.Close();
        }

        private void 登录首页ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DocMain f = new DocMain();
            f.Show();
            this.Close();
        }

        private void button1_Click_2(object sender, EventArgs e)
        {
            SqlCommand sqlCommand1 = new SqlCommand();                                                       //声明并实例化SQL命令；
            sqlCommand1.Connection = sqlHelper.sqlConnection;                                                          //将SQL命令的连接属性指向SQL连接；
            sqlCommand1.CommandText = "SELECT Guomingshi FROM tb_Patient where No='" + tb_no.Text  + "'";
            sqlHelper.sqlConnection.Open();
            tb_guomingshi .Text =Convert.ToString(sqlCommand1.ExecuteScalar());
            SqlCommand sqlCommand2 = new SqlCommand();                                                       //声明并实例化SQL命令；
            sqlCommand2.Connection = sqlHelper.sqlConnection;                                                          //将SQL命令的连接属性指向SQL连接；
            sqlCommand2.CommandText = "SELECT Bingshi FROM tb_Patient where No='" + tb_no.Text + "'";
           
            tb_bingshi.Text  = Convert.ToString(sqlCommand2.ExecuteScalar());
            sqlHelper.sqlConnection.Close();

        }
    }
}
